#include "common/api/server.h"

#include "common/log/crow_logger.hpp"

#include "common/more/uuid.h"

#include "common/config/config.h"

#include "session/permission_manager.h"

#include "get_ids_handler.h"
#include "get_elements_handler.h"
#include "add_element_handler.h"
#include "set_element_handler.h"
#include "del_element_handler.h"
#include "has_changes_handler.h"
#include "login_handler.h"
#include "logout_handler.h"

#include "api_controller.h"

namespace Api
{

ApiController::ApiController()
    : m_crowApplication(new crow::SimpleApp)
    , m_getIdsHandler(new GetIdsHandler((*m_crowApplication), this))
    , m_getElementsHandler(new GetElementsHandler((*m_crowApplication), this))
    , m_addElementHandler(new AddElementHandler((*m_crowApplication), this))
    , m_setElementHandler(new SetElementHandler((*m_crowApplication), this))
    , m_delElementHandler(new DelElementHandler((*m_crowApplication), this))
    , m_hasChangesHandler(new HasChangesHandler((*m_crowApplication), this))
    , m_loginHandler(new LoginHandler((*m_crowApplication), this))
    , m_logoutHandler(new LogoutHandler((*m_crowApplication), this))
{
    static CrowUiLogger logger;
    crow::logger::setHandler(&logger);
}

ApiController::~ApiController()
{
}

void ApiController::run()
{
    m_crowApplication->port(CONFIG.network.apiPort).multithreaded().run();
}

std::shared_ptr<DataController> ApiController::dataController() const
{
    return m_dataController;
}

void ApiController::setDataController(
    std::shared_ptr<DataController> dataController
)
{
    m_dataController = dataController;
}

std::shared_ptr<PermissionManager> ApiController::permissionManager() const
{
    return m_permissionManager;
}

void ApiController::setPermissionManager(
    std::shared_ptr<PermissionManager> permissionManager
)
{
    m_permissionManager = permissionManager;
}

ChangesManager& ApiController::changesManager()
{
    return m_changesManager;
}

} // namespace Api
